/*
 * main.cpp
 *
 *  Created on: Nov 14, 2012
 *      Author: Pongsathon Janyoi
 *  Created arpa prob in binary file.
 */
#include <stdio.h>
#include <stdlib.h>

typedef struct _prob_bi_struct{
	float p;
	float bo;
}prob_bi_struct;
prob_bi_struct ** pron_st;
int pron_st_len=0;
int make_binary(const char * input,const char * output){
	printf("ARPA PROP Index Binary Structure.\n");
	printf("Text Input : %s\n",input);
	printf("Binary Output : %s\n",output);
	printf("DAT Dict Output : %s.dic\n",output);

	char filename[512];
	sprintf(filename,"%s.dic",output);
	FILE * fp = fopen(input,"r");
	FILE * fw = fopen(output,"wb");
	FILE * ft = fopen(filename,"w");
	char prefix[100];
	float _p;
	float _bo;

	while(!feof(fp)){
		fscanf(fp,"%s\t%f\t%f\n",prefix,&_p,&_bo);
		//printf("%s\n",prefix);
		prob_bi_struct st;
		st.p = _p;
		st.bo = _bo;
		fwrite(&st,sizeof(prob_bi_struct),1,fw);
		fprintf(ft,"%s\n",prefix);

	}
    fclose(fp);
    fclose(fw);
    fclose(ft);
	return 0;
}
int test_read(const char* input){
	pron_st = (prob_bi_struct **)calloc(5024,sizeof(prob_bi_struct *));
	FILE * fp = fopen(input,"r");


	while(!feof(fp)){
		prob_bi_struct st ;
		fread(&st,sizeof(prob_bi_struct),1,fp);
		prob_bi_struct * new_st = (prob_bi_struct *)malloc(sizeof(prob_bi_struct));
		new_st->p = st.p;
		new_st->bo = st.bo;
		pron_st[pron_st_len] = new_st;
		pron_st_len++;
	}


/*
	printf("\ni = %d",pron_st_len);
	for(int i=0;i<pron_st_len;i++){
		printf("%d\t%.4f\t%.4f",i,pron_st[i]->p,pron_st[i]->bo);
		printf("\n");
	}*/
}
int main(int argc,char * argv[]){
	if(argc<3){
		printf("usege : ./arpa_prob <input> <output>");
		exit(0);
	}
	make_binary(argv[1],argv[2]);
	test_read(argv[2]);
	printf("End.");
	return 0;
}


